#include "stdafx.h"
#include "OptionDetailsFrame.h"
#include "GlobalDef.h"
#include "OptionItem.h"

const LPCTSTR COptionDetailsFrame::m_pClassName = _T("OPTION_DETAILS");

COptionDetailsFrame::COptionDetailsFrame()
	: m_pRegistryKey(nullptr)
	, m_btnClose(nullptr)
{
}


COptionDetailsFrame::~COptionDetailsFrame()
{
}

DuiLib::CDuiString COptionDetailsFrame::GetSkinFile()
{
	return _T("option_details_frame.xml");
}

LPCTSTR COptionDetailsFrame::GetWindowClassName(void) const
{
	return m_pClassName;
}

void COptionDetailsFrame::Notify(TNotifyUI& msg)
{
	if (msg.sType == DUI_MSGTYPE_CLICK)
	{
		CDuiString strName = msg.pSender->GetName();
		if (strName == _T("btn_close"))
		{
			HWND hWndParent = GetWindowOwner(m_hWnd);
			if (hWndParent)
			{
				::EnableWindow(hWndParent, TRUE);
				::SetFocus(hWndParent);
			}
			ShowWindow(false);
		}
	}

	__super::Notify(msg);
}

void COptionDetailsFrame::InitWindow()
{
	m_textTitle = dynamic_cast<CTextUI*>(FindControl(_T("title_tip")));
	m_btnClose = dynamic_cast<CButtonUI*>(FindControl(_T("btn_close")));
	m_pListUI = dynamic_cast<CListUI*>(FindControl(_T("details_wnd_list")));
}

LRESULT COptionDetailsFrame::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	::PostQuitMessage(0L);
	bHandled = FALSE;
	return 0;
}

LRESULT COptionDetailsFrame::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = __super::HandleMessage(uMsg, wParam, lParam);
	return lRes;
}

void COptionDetailsFrame::SetOptionRegistryKey(LPCTSTR lpRegistryKey)
{
	m_pRegistryKey = lpRegistryKey;
	EnumOptions();
}

void COptionDetailsFrame::EnumOptions()
{
	CDuiString strSubKey = _T("");
	if (m_pRegistryKey == _T("CleanAutoruns"))
	{
		strSubKey = SYSTEM_OPTIMIZER_AUTORUNS;
	}
	else if (m_pRegistryKey == _T("CleanRightMenuItems"))
	{
		strSubKey = SYSTEM_OPTIMIZER_RIGHTMENUITEMS;
	}
	else if (m_pRegistryKey == _T("CleanSystemTempFiles"))
	{
		strSubKey = SYSTEM_OPTIMIZER_SYSTEMTEMPFILES;
	}
	else if (m_pRegistryKey == _T("CleanSystemEventLogs"))
	{
		strSubKey = SYSTEM_OPTIMIZER_SYSTEEVENTLOGS;
	}
	else if (m_pRegistryKey == _T("CleanScheduledTasks"))
	{
		strSubKey = SYSTEM_OPTIMIZER_SCHEDULEDTASKS;
	}
	else if (m_pRegistryKey == _T("DisableSystemServices"))
	{
		strSubKey = SYSTEM_OPTIMIZER_SYSTEMSERVICES;
	}
	else if (m_pRegistryKey == _T("DisableHardwareDevices"))
	{
		strSubKey = SYSTEM_OPTIMIZER_HARDWAREDEVICES;
	}
	else if (m_pRegistryKey == _T("RemovedGhostDevices"))
	{
		strSubKey = SYSTEM_OPTIMIZER_GHOSTDEVICES;
	}

	m_pListUI->RemoveAll();

	CRegKey regKey;
	LONG lResult;
	lResult = regKey.Open(HKEY_CURRENT_USER, strSubKey);
	if (ERROR_SUCCESS != lResult)
	{
		return;
	}

	DWORD dwIndex = 0;
	TCHAR szKeyName[MAX_PATH * 2] = { 0 };
	DWORD dwSize = MAX_PATH * 2;

	while (ERROR_SUCCESS == RegEnumValue(regKey.m_hKey, dwIndex++,
		szKeyName,
		&dwSize,
		nullptr,
		nullptr,
		nullptr,
		nullptr))
	{
		COptionItem* item = new COptionItem(m_pListUI, &m_PaintManager);
		item->InitControl(strSubKey, szKeyName, FALSE);

		ZeroMemory(szKeyName, MAX_PATH * 2);
		dwSize = MAX_PATH * 2;
	}

	regKey.Close();
}

LRESULT COptionDetailsFrame::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
{
	if (uMsg == WM_KEYDOWN)
	{
		switch (wParam)
		{
		case VK_RETURN:
		case VK_ESCAPE:
			return FALSE;
		default:
			break;
		}
	}

	return FALSE;
}

